﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sealight.Core.DataAdapter;
using log4net;
using Sealight.API.Framework;
using Sealight.UI.WebUI.Models;
using Telerik.Web.Mvc;
using Telerik.Web.Mvc.UI;
using System.Collections;
using System.Globalization;
using Sealight.UI.WebUI.Services;
namespace Sealight.UI.WebUI.Areas.Admin.Controllers
{
    [Sealight.UI.WebUI.UIHelpers.SessionExpireFilter]
    [SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
    //[OutputCache(NoStore = true, Duration = 60, VaryByParam = "*")]
    public class ProductController : Controller
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(ProductController));
        #region UI
        [Authorize]
        public ActionResult Index()
        {
            return View();
        }
        [Authorize]
        public ActionResult AddUpdateProductAttributePage()
        {
            if (Request["id"] != null)
            {
                try
                {
                    var data = Services.ProductService.GetProductAttribute(new Guid(Request["id"].ToString()));
                    return View("_AddUpdateProductAttribute", data);
                }
                catch { }
            }
            return View("_AddUpdateProductAttribute");
        }
        [Authorize]
        public ActionResult ManageProductAttributePage()
        {
            int currentPage = 0, totalPage = 0, sizePage = 5;

            if (Request["currentPage"] != null)
            {
                try
                {
                    int.TryParse(Request["currentPage"].ToString(), out currentPage);
                }
                catch { }
            }

            ObjectPagingModel<product_attribute> model = new ObjectPagingModel<product_attribute>();

           /* var data = Services.ProductService.GetAllProductAttribute();

            if (data != null)
            {
                totalPage = data.Count / sizePage + (data.Count % sizePage > 0 ? 1 : 0);
                model.PadingPage = Sealight.UI.WebUI.UIHelpers.CommonHelper.GetPageListViewForFronEnd(currentPage, totalPage, "/admin/product/ManageProductAttributePage");
                model.Data = data.Skip(currentPage*sizePage).Take(sizePage).ToList();
                model.TotalPage = totalPage;
                model.CurrentPage = currentPage;
            }
            */
            return View("_ManageProductAtribute", model);
        }
        [Authorize]
        public ActionResult AddUpdateSpecificationAttributePage()
        {
            return View("_AddUpdateSpecificationAttribute");
        }
        [Authorize]
        public ActionResult ManageSpecificationAttributePage()
        {
            return View("_ManageSpecificationProductAttribute");
        }
        [Authorize]
        public ActionResult ManageCategoryPage()
        {
            IList<product_category> data = new List<product_category>();
            data = Services.ProductService.GetAllCategoryByParentId(Guid.Empty);
            return View("_ManageCategory",data);
        }
        
        [Authorize]
        public ActionResult AddUpdateCategoryPage(string id)
        {
            Guid catId = Guid.Empty;
            product_category data = null;
            if (!string.IsNullOrEmpty(id))
            {
                Guid.TryParse(id, out catId);
                if (catId != Guid.Empty)
                {
                    data = Services.ProductService.GetCategoryById(catId);
                }
            }
            return View("_AddUpdateCategory", data);
        }
        [Authorize]
        public ActionResult ManageAttributeOption(string id)
        {
            ViewData["spec_attribute_id"] = id;
            return View("_ManageSpecificationAttributeOption");
        }
        #endregion
        #region Data
        [Authorize]
        public ActionResult SaveProductAttributeData(product_attribute data)
        {
            AjaxOperationResponse response = new AjaxOperationResponse();
            try
            {
                //data = Services.ProductService.AddUpdateProductAttribute(data);
            }
            catch (Exception ex)
            {
                log.Error("Error while SaveProductAttributeData.", ex);
                response.OperationComplete = false;
                response.ErrorCode = "4001";
                response.ErrorFriendlyMessage = string.Format("{0}", UIHelpers.Message.Error);
            }

            return Json(data);
        }

        [Authorize]
        public ActionResult DeleteProductAttributeData()
        {
            AjaxOperationResponse response = new AjaxOperationResponse();
            try
            {
                if (Request["id"] != null)
                {
                    Services.ProductService.DeleteProductAttribute(new Guid(Request["id"].ToString()));
                }               
            }
            catch (Exception ex)
            {
                log.Error("Error while SaveProductAttributeData.", ex);
                response.OperationComplete = false;
                response.ErrorCode = "4001";
                response.ErrorFriendlyMessage = string.Format("{0}", UIHelpers.Message.Error);
                return Json(response);
            }
            return Json("true");
        }
        #region category
        public ActionResult _LoadProductCategory(TreeViewItem node)
        {
            IList<product_category> data = new List<product_category>();

            Guid parentId = !string.IsNullOrEmpty(node.Value) ? Guid.Parse(node.Value) : Guid.Empty;

            data = Services.ProductService.GetAllCategoryByParentId(parentId);
            IEnumerable nodes = from item in data
                                select new TreeViewItem
                                {
                                    Text = item.name,
                                    Value = item.id.ToString(),
                                    LoadOnDemand = item.childrens.Count > 0,
                                    Enabled = true
                                };
            return new JsonResult { Data = nodes };

        }
        [Authorize]
        public ActionResult _SaveCategoryData(product_category data)
        {
            AjaxOperationResponse response = new AjaxOperationResponse();
            try
            {
                if (data != null)
                {
                    data = Services.ProductService.AddOrUpdateCategory(data);
                }
            }
            catch (Exception ex)
            {
                log.Error("Error while SaveProductAttributeData.", ex);
                response.OperationComplete = false;
                response.ErrorCode = "4001";
                response.ErrorFriendlyMessage = string.Format("{0}", UIHelpers.Message.Error);
            }

            return Json(data);
        }
        [Authorize]
        public ActionResult ManageCategoryMain()
        {
           var  data = Services.ProductService.GetAllProductCategory();
           return View("_ManageCategoryMain", data);
        }
        [Authorize]
        public ActionResult _DeleteCategory()
        {
            Guid id = Guid.Empty;
            if (Request["id"] != null)
            {
                Guid.TryParse(Request["id"], out id);
              
            }
            Services.ProductService.DeleteCategory(id);
            return Json("true");
        }
        #endregion
        #region SpecificationProduct
        [GridAction]
        public ActionResult _SpecificationProductSelectAjaxEditing()
        {
            var data = Services.ProductService.GetAllSpecificationProductAttribute();
            return View(new GridModel(data));
        }
        [AcceptVerbs(HttpVerbs.Post)]        
        [GridAction]
        public ActionResult _SpecificationProductSaveAjaxEditing(string id)
        {
            Guid s_id = Guid.Parse(id);
            SpecificationAttributeViewModel data =new SpecificationAttributeViewModel();

            TryUpdateModel(data);
            Services.ProductService.AddUpdateSpecificationAttribute(data);
            var result = Services.ProductService.GetAllSpecificationProductAttribute();
            return View(new GridModel(result));
        }
        [AcceptVerbs(HttpVerbs.Post)]      
        [GridAction]
        public ActionResult _SpecificationProductInsertAjaxEditing()
        {
            //Create a new instance of the EditableProduct class.
            SpecificationAttributeViewModel data = new SpecificationAttributeViewModel();
            //Perform model binding (fill the product properties and validate it).
            if (TryUpdateModel(data))
            {
                //The model is valid - insert the product.    
                Services.ProductService.AddUpdateSpecificationAttribute(data);
            }
            //Rebind the grid
            var result = Services.ProductService.GetAllSpecificationProductAttribute();
            return View(new GridModel(result));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _SpecificationProductDeleteAjaxEditing(string id)
        {
            //
            Guid s_id = Guid.Parse(id);
            if (s_id != Guid.Empty)
            {
                Services.ProductService.DeleteSpecificationAttribute(s_id);
            }
            //Rebind the grid
            var result = Services.ProductService.GetAllSpecificationProductAttribute();
            return View(new GridModel(result));
        }

        #endregion

        #region Product Attribute
        [GridAction]
        public ActionResult _ProductAttributeSelectAjaxEditing()
        {
            var data = Services.ProductService.GetAllProductAttribute();
            return View(new GridModel(data));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _ProductAttributeSaveAjaxEditing(string id)
        {
            Guid s_id = Guid.Parse(id);
            ProductAttributeViewModel data = new ProductAttributeViewModel();

            TryUpdateModel(data);
            Services.ProductService.AddUpdateProductAttribute(data);
            var result = Services.ProductService.GetAllProductAttribute();
            return View(new GridModel(result));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _ProductAttributeInsertAjaxEditing()
        {
            //Create a new instance of the EditableProduct class.
            ProductAttributeViewModel data = new ProductAttributeViewModel();
            //Perform model binding (fill the product properties and validate it).
            if (TryUpdateModel(data))
            {
                //The model is valid - insert the product.    
                Services.ProductService.AddUpdateProductAttribute(data);
            }
            //Rebind the grid
            var result = Services.ProductService.GetAllProductAttribute();
            return View(new GridModel(result));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _ProductAttributeDeleteAjaxEditing(string id)
        {
            //
            Guid s_id = Guid.Parse(id);
            if (s_id != Guid.Empty)
            {
                Services.ProductService.DeleteProductAttribute(s_id);
            }
            //Rebind the grid
            var result = Services.ProductService.GetAllProductAttribute();
            return View(new GridModel(result));
        }
        #endregion

        #region Specification Attribute Option
        [GridAction]
        public ActionResult _SpecificationAttributeOptionSelectAjaxEditing(string specId)
        {
            var data = Services.ProductService.GetAllOptionBySpecificationId(new Guid(specId));
            return View(new GridModel(data));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _SpecificationAttributeOptionSaveAjaxEditing(string id, string specId)
        {
            Guid s_id = Guid.Parse(id);
            SpecificationAttributeOptionViewModel data = new SpecificationAttributeOptionViewModel();

            TryUpdateModel(data);
            if (data.SpecificationAttribute == null)
            {
                data.SpecificationAttribute = new SpecificationAttributeViewModel();
            }
            data.SpecificationAttribute.ID = new Guid(specId);
            Services.ProductService.AddUpdateSpecificationAttributeOption(data);
            var result = Services.ProductService.GetAllOptionBySpecificationId(new Guid(specId));
            return View(new GridModel(result));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _SpecificationAttributeOptionInsertAjaxEditing(string specId)
        {
            //Create a new instance of the EditableProduct class.
            SpecificationAttributeOptionViewModel data = new SpecificationAttributeOptionViewModel();
            //Perform model binding (fill the product properties and validate it).
            if (TryUpdateModel(data))
            {
                if (data.SpecificationAttribute == null)
                {
                    data.SpecificationAttribute = new SpecificationAttributeViewModel();
                }
                data.SpecificationAttribute.ID = new Guid(specId);
                //The model is valid - insert the product.    
                Services.ProductService.AddUpdateSpecificationAttributeOption(data);
            }
            //Rebind the grid
            var result = Services.ProductService.GetAllOptionBySpecificationId(new Guid(specId));
            return View(new GridModel(result));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _SpecificationAttributeOptionDeleteAjaxEditing(string id, string specId)
        {
            //
            Guid s_id = Guid.Parse(id);
            if (s_id != Guid.Empty)
            {
                Services.ProductService.DeleteSpecificationAttributeOption(s_id);
            }
            //Rebind the grid
            var result = Services.ProductService.GetAllOptionBySpecificationId(new Guid(specId));
            return View(new GridModel(result));
        }
        #endregion

        #region Product
        [Authorize]
        public ActionResult ManageProductPage(string keyword, string category)
        {
            ViewData["keyword"] = keyword??string.Empty;
            ViewData["category"] = category ?? string.Empty;
            return View("_ManageProduct");
        }
        [Authorize]
        public ActionResult AddUpdateProduct(Guid? id)
        {
            product data = null;
            if (id.HasValue)
            {
                data = Services.ProductService.GetProduct(id.Value);
            }
            return View("_AddUpdateProduct", data);
        }
        
        [Authorize]
        public ActionResult _SaveProductData(product data)
        {
            AjaxOperationResponse response = new AjaxOperationResponse();
            try
            {
                if (data != null)
                {
                    if (data.id != Guid.Empty)
                    {                      
                        data.modified_date = DateTime.Now;
                    }
                    else
                    {
                        data.created_date = DateTime.Now;
                        data.modified_date = DateTime.Now;
                    }

                    if (data.id == Guid.Empty)
                    {
                        if (data.base_variant != null)
                        {
                            data.base_variant.created_date = DateTime.Now;
                            data.base_variant.modified_date = DateTime.Now;
                            if (!string.IsNullOrEmpty(data.base_variant.input_available_start_date))
                            {
                                data.base_variant.available_start_date_time = DateTime.ParseExact(data.base_variant.input_available_start_date, "dd/MM/yyyy", CultureInfo.CurrentCulture);
                            }
                            if (!string.IsNullOrEmpty(data.base_variant.input_available_end_date))
                            {
                                data.base_variant.available_end_date_time = DateTime.ParseExact(data.base_variant.input_available_end_date, "dd/MM/yyyy", CultureInfo.CurrentCulture);
                            }
                        }
                    }
                    data = Services.ProductService.AddOrUpdateProduct(data);
                }
            }
            catch (Exception ex)
            {
                log.Error("Error while _SaveProductData.", ex);
                response.OperationComplete = false;
                response.ErrorCode = "4001";
                response.ErrorFriendlyMessage = string.Format("{0}", UIHelpers.Message.Error);
            }

            return Json(data);
        }
        [Authorize]
        public ActionResult ListCategoryForMapToProduct()
        {
            var data = Services.ProductService.GetAllProductCategory();
            return View("_CategoryMapForProduct", data);
        }
        [Authorize]
        public ActionResult _SaveCategoryMapToProduct(IList<product_category_map> data)
        {
            Guid productId = Guid.Empty;

            if (Request["id"] != null)
            {
                Guid.TryParse(Request["id"].ToString(), out productId);
            }

             AjaxOperationResponse response = new AjaxOperationResponse();
            try
            {
                if (productId != Guid.Empty && data != null)
                {
                  var returnData =  Services.ProductService.SaveCategoryToProduct(data, productId);

                  return Json(returnData);
                }
            }
            catch (Exception ex)
            {
                log.Error("Error while _SaveCategoryMapToProduct.", ex);
                response.OperationComplete = false;
                response.ErrorCode = "4001";
                response.ErrorFriendlyMessage = string.Format("{0}", UIHelpers.Message.Error);
            }
            return Json(response);
        }
        [Authorize]
        public ActionResult _SavePictureForProduct()
        {
            Guid productId = Guid.Empty;
            string image_name = string.Empty;

            if (Request["productId"] != null)
            {
                Guid.TryParse(Request["productId"].ToString(), out productId);
            }
            if (Request["imageName"] != null)
            {
                image_name = Request["imageName"].ToString();
            }

            AjaxOperationResponse response = new AjaxOperationResponse();
            try
            {
                if (productId != Guid.Empty && !string.IsNullOrEmpty(image_name))
                {
                  var data = Services.ProductService.SavePictureForProduct(productId, image_name);
                  return Json(data);
                }
            }
            catch (Exception ex)
            {
                log.Error("Error while _SavePictureForProduct.", ex);
                response.OperationComplete = false;
                response.ErrorCode = "4001";
                response.ErrorFriendlyMessage = string.Format("{0}", UIHelpers.Message.Error);
            }
            return Json("1");
        }
        [Authorize]       
        public ActionResult ListRelateProduct(Guid productId)
        { 
            IList<related_product> data= null;
            try
            {
                if (productId != Guid.Empty )
                {
                   data = Services.ProductService.GetRelatedProducts(productId);
                }
            }
            catch (Exception ex)
            {
                log.Error("Error while GetRelatedProducts.", ex);               
            }

            return View("_ListRelateProduct", data);
        }
        [Authorize]
        public ActionResult _SaveRelatedProduct(IList<Guid> data)
        {
            Guid productId = Guid.Empty;

            if (Request["id"] != null)
            {
                Guid.TryParse(Request["id"].ToString(), out productId);
            }
            AjaxOperationResponse response = new AjaxOperationResponse();
            try
            {
                if (productId != Guid.Empty && data != null)
                {
                    foreach (var m in data)
                    {
                        related_product item = new related_product
                        {
                            product1 = new product { id = productId },
                            product2 = new product { id= m}

                        };
                        if (item != null)
                        {
                            Services.ProductService.SaveRelatedProduct(item);
                        }
                    }
                }

                return Json("1");
            }
            catch (Exception ex)
            {
                log.Error("Error while _SaveRelatedProduct.", ex);
                response.OperationComplete = false;
                response.ErrorCode = "4001";
                response.ErrorFriendlyMessage = string.Format("{0}", UIHelpers.Message.Error);
            }
            return Json(response);
        }
        [Authorize]
        public ActionResult _SearchProduct()
        {
            string keyword = string.Empty;
            Guid categoryId = Guid.Empty;
            AjaxOperationResponse response = new AjaxOperationResponse();
            try
            {
                if (Request["productName"] != null)
                {
                    keyword = Request["productName"].ToString();
                }
                if (Request["categoryId"] != null)
                {
                    Guid.TryParse(Request["categoryId"].ToString(), out categoryId);
                }

                if (!string.IsNullOrEmpty(keyword))
                {
                   var data =  Services.ProductService.SearchProduct(keyword, categoryId);

                   return Json(data);
                }
            }
            catch (Exception ex)
            {
                log.Error("Error while _SaveRelatedProduct.", ex);
                response.OperationComplete = false;
                response.ErrorCode = "4001";
                response.ErrorFriendlyMessage = string.Format("{0}", UIHelpers.Message.Error);
            }
            return Json(response);
        }
        [GridAction]
        public ActionResult _SearchProductAjax(string keyword, string category)
        {
            Guid categoryId = Guid.Empty;

            Guid.TryParse(category, out categoryId);

            var data = Services.ProductService.SearchProduct(keyword, categoryId);
            
            return View(new GridModel(data));
        }
        [Authorize]
        public ActionResult _DeleteCategoryMapToProduct()
        {
            Guid id = Guid.Empty;

            if (Request["id"] != null)
            {
                Guid.TryParse(Request["id"].ToString(), out id);
            }
            AjaxOperationResponse response = new AjaxOperationResponse();
            try
            {
                if (id != Guid.Empty)
                {
                    Services.ProductService.DeleteCategoryMapToProduct(id);
                }
            }
            catch (Exception ex)
            {
                log.Error("Error while _SaveProductData.", ex);
                response.OperationComplete = false;
                response.ErrorCode = "4001";
                response.ErrorFriendlyMessage = string.Format("{0}", UIHelpers.Message.Error);

                return Json(response);
            }
            return Json("1");
        }
        [Authorize]
        public ActionResult _DeletePictureMapToProduct()
        {
            Guid id = Guid.Empty;
            Guid pid = Guid.Empty;

            if (Request["id"] != null)
            {
                Guid.TryParse(Request["id"].ToString(), out id);
            }
            if (Request["pid"] != null)
            {
                Guid.TryParse(Request["pid"].ToString(), out pid);
            }
            AjaxOperationResponse response = new AjaxOperationResponse();
            try
            {
                if (id != Guid.Empty)
                {
                    Services.ProductService.DeletePictureMapToProduct(id);
                }
            }
            catch (Exception ex)
            {
                log.Error("Error while _DeletePictureMapToProduct.", ex);
                response.OperationComplete = false;
                response.ErrorCode = "4001";
                response.ErrorFriendlyMessage = string.Format("{0}", UIHelpers.Message.Error);

                return Json(response);
            }
            return Json("1");
        }

        [Authorize]
        public ActionResult _RemoveRelatedProduct()
        {           
            Guid relateProductId = Guid.Empty;
            AjaxOperationResponse response = new AjaxOperationResponse();
            
            if (Request["relateProductId"] != null)
            {
                Guid.TryParse(Request["relateProductId"], out relateProductId);
            }
             try
            {
                Services.ProductService.RemoveRelatedProduct(relateProductId);
                return Json("1");
            }
             catch (Exception ex)
             {
                 log.Error("Error while _RemoveRelatedProduct.", ex);
                 response.OperationComplete = false;
                 response.ErrorCode = "4001";
                 response.ErrorFriendlyMessage = string.Format("{0}", UIHelpers.Message.Error);
             }
             return Json(response);

        }
        #endregion

        #region Product Variant
        [Authorize]
        public ActionResult AddUpdateProductVariant(Guid? id)
        {
            product_variant data = null;
            if (id.HasValue)
            {
                data = Services.ProductService.GetProductVariant(id.Value);
            }
            else
            {
                if(Request["productId"]!= null)
                {
                    ViewData["productId"] = Request["productId"].ToString();
                }
            }
            return View("_AddUpdateProductVariant", data);
        }

        [Authorize]
        public ActionResult _SaveProductVariantData(product_variant data)
        {
            AjaxOperationResponse response = new AjaxOperationResponse();
            try
            {
                if (data != null)
                {
                    if (data.id != Guid.Empty)
                    {
                        data.modified_date = DateTime.Now;
                    }
                    else
                    {
                        data.created_date = DateTime.Now;
                        data.modified_date = DateTime.Now;
                    }
                    if (!string.IsNullOrEmpty(data.input_available_start_date))
                    {
                        data.available_start_date_time = DateTime.ParseExact(data.input_available_start_date, "dd/mm/yyyy", CultureInfo.CurrentCulture);
                    }
                    if (!string.IsNullOrEmpty(data.input_available_end_date))
                    {
                        data.available_end_date_time = DateTime.ParseExact(data.input_available_end_date, "dd/mm/yyyy", CultureInfo.CurrentCulture);
                    }
                   
                    data = Services.ProductService.AddOrUpdateProductVariant(data);
                }
            }
            catch (Exception ex)
            {
                log.Error("Error while _SaveProductVariantData.", ex);
                response.OperationComplete = false;
                response.ErrorCode = "4001";
                response.ErrorFriendlyMessage = string.Format("{0}", UIHelpers.Message.Error);
            }

            return Json(data);
        }
        #endregion

        #region Product variant attribute
        [Authorize]
        public ActionResult ListAttributeForProductVariant(Guid productVariantId)
        {
            ViewData["productVariantId"] = productVariantId;
            return View("_ListAttributeForProductVariant");
        }
        [GridAction]
        public ActionResult _ProductVariantAttributeSelectAjaxEditing(string productVariantId)
        {
            IList<ProductVariantAttributeViewModel> data = new List<ProductVariantAttributeViewModel>();

            if (!string.IsNullOrEmpty(productVariantId))
            {
                data = Services.ProductService.GetProductVariantAttribute(new Guid(productVariantId));
            }
            return View(new GridModel(data));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _ProductVariantAttributeSaveAjaxEditing(string id, string productVariantId)
        {
            Guid s_id = Guid.Parse(id);
            ProductVariantAttributeViewModel data = new ProductVariantAttributeViewModel();

            TryUpdateModel(data);
            data.ProductVariantAttributeID = s_id;
            data.ProductVariantID = new Guid(productVariantId);
            if (data != null && !string.IsNullOrEmpty(data.ProductAttribute))
            {
                Services.ProductService.AddProductVariantAttribute(data);
            }
            var result = Services.ProductService.GetProductVariantAttribute(new Guid(productVariantId));
            return View(new GridModel(result));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _ProductVariantAttributeInsertAjaxEditing(string productVariantId)
        {
            //Create a new instance of the EditableProduct class.
            ProductVariantAttributeViewModel data = new ProductVariantAttributeViewModel();
            //Perform model binding (fill the product properties and validate it).
            if (TryUpdateModel(data))
            {
                data.ProductVariantID =new Guid( productVariantId);
                if (data != null && !string.IsNullOrEmpty(data.ProductAttribute))
                {
                    Services.ProductService.AddProductVariantAttribute(data);
                }
            }
            //Rebind the grid
            var result = Services.ProductService.GetProductVariantAttribute(new Guid(productVariantId));
            return View(new GridModel(result));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _ProductVariantAttributeDeleteAjaxEditing(string id, string productVariantId)
        {
            //
            Guid s_id = Guid.Parse(id);
            if (s_id != Guid.Empty)
            {
                Services.ProductService.DeleteProductVariantAttribute(s_id);
            }
            //Rebind the grid
            var result = Services.ProductService.GetProductVariantAttribute(new Guid(productVariantId));
            return View(new GridModel(result));
        }
        #endregion

        #region Product Variant attribute value
        [Authorize]
        public ActionResult ListAttributeValueForProductVariantAttribute(Guid id)
        {
            ViewData["productVariantAttributeId"] = id;
            return View("_ListProductVariantAttributeValue");
        }
        [GridAction]
        public ActionResult _ProductVariantAttributeValueSelectAjaxEditing(string attributeId)
        {
            IList<ProductVariantAttributeValueViewModel> data = new List<ProductVariantAttributeValueViewModel>();

            if (!string.IsNullOrEmpty(attributeId))
            {
                data = Services.ProductService.GetProductVariantAttributeValue(new Guid(attributeId));
            }
            return View(new GridModel(data));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _ProductVariantAttributeValueSaveAjaxEditing(string id, string attributeId)
        {
            Guid s_id = Guid.Parse(id);
            ProductVariantAttributeValueViewModel data = new ProductVariantAttributeValueViewModel();

            TryUpdateModel(data);
            data.ProductVariantAttributeID = new Guid(attributeId);
            data.ProductVariantAttributeValueID = s_id;
            if (data != null && data.ProductVariantAttributeID != Guid.Empty)
            {
                Services.ProductService.AddProductVariantAttributeValue(data);
            }
            var result = Services.ProductService.GetProductVariantAttributeValue(new Guid(attributeId));
            return View(new GridModel(result));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _ProductVariantAttributeValueInsertAjaxEditing(string attributeId)
        {
            //Create a new instance of the EditableProduct class.
            ProductVariantAttributeValueViewModel data = new ProductVariantAttributeValueViewModel();
            //Perform model binding (fill the product properties and validate it).
            if (TryUpdateModel(data))
            {
                data.ProductVariantAttributeID =new Guid( attributeId);
                if (data != null && data.ProductVariantAttributeID != Guid.Empty)
                {
                    Services.ProductService.AddProductVariantAttributeValue(data);
                }
            }
            //Rebind the grid
            var result = Services.ProductService.GetProductVariantAttributeValue(new Guid(attributeId));
            return View(new GridModel(result));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _ProductVariantAttributeValueDeleteAjaxEditing(string id, string attributeId)
        {
            Guid s_id = Guid.Parse(id);
            if (s_id != Guid.Empty)
            {
                Services.ProductService.DeleteProductVariantAttributeValue(s_id);
            }
            //Rebind the grid
            var result = Services.ProductService.GetProductVariantAttributeValue(new Guid(attributeId));
            return View(new GridModel(result));
        }
        #endregion

        #region Specification Product Option
        [Authorize]
        public ActionResult AddUpdateProductSpecificationAttributeOption(string psid, string pid)
        {
            ProductSpecificationAttributeViewModel data= null;
            if (!string.IsNullOrEmpty(psid))
            {
                Guid id = Guid.Empty;
                Guid.TryParse(psid, out id);
                data =Services.ProductService.GetProductSpecificationAttribute(id);
            }
            ViewData["productId"] = pid;
            return View("_AddUpdateProductSpecificationOption", data);
        }
        [Authorize]
        public ActionResult ListSpecificationAttributeOptionForProduct(Guid productId)
        {
            ViewData["productId"] = productId;
            return View("_MapSpecificationAttributeForProduct");
        }
        [Authorize]
        public ActionResult GetListOptionForSpecificationAttr()
        {
            Guid id = Guid.Empty;
            IList<SpecificationAttributeOptionViewModel> data = null;
            if (Request["id"] != null)
            {
                Guid.TryParse(Request["id"].ToString(), out id);
            }
            if (id != Guid.Empty)
            {
                data = Services.ProductService.GetAllOptionBySpecificationId(id);
            }
            return Json(data);
        }
        [Authorize]
        public string SaveUpdateProductSpecificationAttributeOption(string specificationAttribute, string attributeOption, string allowFilter,
            string showProductPage, int displayOrder, string productId, string psId)
        {

            if (!string.IsNullOrEmpty(productId))
            {
                Guid specAttrId = Guid.Parse(specificationAttribute);
                Guid attrOpId = Guid.Parse(attributeOption);
                Guid proId = Guid.Parse(productId);

                Guid id = Guid.Empty;

                Guid.TryParse(psId, out id);
                ProductSpecificationAttributeViewModel data = new ProductSpecificationAttributeViewModel();
                data.ShowOnProductPage =string.IsNullOrEmpty(showProductPage)?false:( showProductPage.ToLower() == "on" ? true: (showProductPage.ToLower()=="off" ? false: bool.Parse(showProductPage)));
                data.AllowFiltering =string.IsNullOrEmpty(allowFilter)?false:( allowFilter.ToLower() == "on" ? true : (allowFilter.ToLower() == "off" ? false : bool.Parse(allowFilter)));
                data.DisplayOrder = displayOrder;
                data.SpecificationAttributeID = specAttrId;
                data.SpecificationAttributeOptionID = attrOpId;
                data.ProductID = proId;
                data.ID = id;
                if (data != null)
                {
                    Services.ProductService.MapProductSpecificationAttribute(data);
                }

            }

            return "Save Successfully";
        }
        [GridAction]
        public ActionResult _SpecificationOptionForProductSelectAjaxEditing(string productId)
        {
            IList<ProductSpecificationAttributeViewModel> data = new List<ProductSpecificationAttributeViewModel>();

            if (!string.IsNullOrEmpty(productId))
            {
                data = Services.ProductService.GetSpecificationOptionAttributeForProduct(new Guid(productId));
            }
            return View(new GridModel(data));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _SpecificationOptionForProductSaveAjaxEditing(string id, string productId)
        {
            Guid s_id = Guid.Parse(id);
            ProductSpecificationAttributeViewModel data = new ProductSpecificationAttributeViewModel();

            TryUpdateModel(data);
            data.ID = s_id;
            data.ProductID = new Guid(productId);
            if (data != null)
            {
                Services.ProductService.MapProductSpecificationAttribute(data);
            }
            var result = Services.ProductService.GetSpecificationOptionAttributeForProduct(new Guid(productId));
            return View(new GridModel(result));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _SpecificationOptionForProductInsertAjaxEditing(string productId)
        {
            //Create a new instance of the EditableProduct class.
            ProductSpecificationAttributeViewModel data = new ProductSpecificationAttributeViewModel();
            //Perform model binding (fill the product properties and validate it).
            if (TryUpdateModel(data))
            {
                data.ProductID = new Guid(productId);
                if (data != null)
                {
                    Services.ProductService.MapProductSpecificationAttribute(data);
                }
            }
            //Rebind the grid
            var result = Services.ProductService.GetSpecificationOptionAttributeForProduct(new Guid(productId));
            return View(new GridModel(result));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _SpecificationOptionForProductDeleteAjaxEditing(string id, string productId)
        {
            //
            Guid s_id = Guid.Parse(id);
            if (s_id != Guid.Empty)
            {
                Sealight.UI.WebUI.Services.ProductService.DeleteProductSpecificationAttribute(s_id);
            }
            //Rebind the grid
            var result = Services.ProductService.GetSpecificationOptionAttributeForProduct(new Guid(productId));
            return View(new GridModel(result));
        }

        #endregion
        #endregion
    }
}
